package auto.app.service.impl;

import auto.app.controller.from.ParamsAddFrom;
import auto.app.controller.from.ParamsAddFromJson;
import auto.app.controller.from.ParamsAddOneFrom;
import auto.app.controller.from.ParamsUpdateFrom;
import auto.app.controller.info.ParamsInfo;
import auto.app.exetion.MainExection;
import auto.app.model.CaseInterface;
import auto.app.model.Params;
import auto.app.dao.ParamsDao;
import auto.app.service.CaseService;
import auto.app.service.ParamsService;
import auto.app.util.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static auto.app.exetion.MainExetionMessage.*;

/**
 * <p>
 * 请求参数信息表 服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-07-09
 */
@Service
public class ParamsServiceImpl extends ServiceImpl<ParamsDao, Params> implements ParamsService {

    @Autowired
    private CaseService caseService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addParams(ParamsAddFrom paramsAddFrom) throws MainExection {
        //获取列表
        List<ParamsAddOneFrom> paramsList = paramsAddFrom.getParamsList();
        //判断用例id是否存在
        CaseInterface caseServiceById = caseService.getById(paramsAddFrom.getCaseId());
        if (caseServiceById == null) {
            throw new MainExection(CASEERROR.getCode(), CASEERROR.getMsg());
        }
        //将参数赋值到model类中
        List<Params> params = CommonUtil.copyList(paramsList, Params.class);
        //将用例id赋值进集合中的参数
        params.stream().forEach(params1 -> params1.setCaseId(paramsAddFrom.getCaseId()));
        //将参数进行入库
        try {
            boolean b = this.saveBatch(params);
            return b;
        } catch (Exception e) {
            throw new MainExection(CASEERROR.getCode(), CASEERROR.getMsg());
        }
    }

    /**
     * 新增参数JSON形式
     * @param paramsAddFromJson
     * @return
     */
    @Override
    public Boolean addParamsJson(ParamsAddFromJson paramsAddFromJson) throws MainExection {
        //判断用例id是否存在
        CaseInterface caseServiceById = caseService.getById(paramsAddFromJson.getCaseId());
        if (caseServiceById == null) {
            throw new MainExection(CASEERROR.getCode(), CASEERROR.getMsg());
        }
        //TODO JSON字符串转为map 这边设计有问题
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Integer id) throws MainExection {
        //先查询params的id 是否存在
        Params params = baseMapper.selectById(id);
        if (params == null) {
            throw new MainExection(PARAMSSELECTERROR.getCode(), PARAMSSELECTERROR.getMsg());
        }
        try {
            int i = baseMapper.deleteById(id);
            return i > 0 ? true : false;
        } catch (Exception e) {
            throw new MainExection(CASEDELETEPARMSERROR.getCode(), CASEDELETEPARMSERROR.getMsg());
        }
    }

    @Override
    public ParamsInfo list(Integer caseid) throws MainExection {
        //判断用例id是否存在
        CaseInterface caseInterface = caseService.getById(caseid);
        if (caseInterface == null) {
            throw new MainExection(CASEERROR.getCode(), CASEERROR.getMsg());
        }
        //用例id存在的话查询参数 返回参数列表
        QueryWrapper<Params> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Params> wrapper = queryWrapper.eq("case_id", caseid);
        List<Params> paramsList = this.list(wrapper);
        return ParamsInfo.builder().paramsList(paramsList).build();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(ParamsUpdateFrom paramsUpdateFrom) throws MainExection {
        //查询参数是否存在
        Params params = baseMapper.selectById(paramsUpdateFrom.getId());
        if (params == null) {
            throw new MainExection(PARAMSSELECTERROR.getCode(), PARAMSSELECTERROR.getMsg());
        }
        //存在的情况下进行参数修改
        Params one = CommonUtil.copyOne(paramsUpdateFrom, Params.class);
        try {
            int i = baseMapper.updateById(one);
            return true;
        } catch (Exception e) {
            throw new MainExection(PARAMSUPDATEERROR.getCode(), PARAMSUPDATEERROR.getMsg());
        }
    }

    @Override
    public Params detail(Integer id) throws MainExection {
        //查询参数是否存在
        Params params = baseMapper.selectById(id);
        if (params == null) {
            throw new MainExection(PARAMSSELECTERROR.getCode(), PARAMSSELECTERROR.getMsg());
        }
        return params;
    }


}
